﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;

namespace EvaluationObjectCalculator
{
    public partial class PingBiaoForm1 : Form 
    {
        int dgvCount = 0;
        public PingBiaoForm1()
        {
            InitializeComponent();
            this.dataGridView1.RowCount = 300;

            //添加序号
            for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
            {
                DataGridViewRow r = this.dataGridView1.Rows[i];
                r.HeaderCell.Value = string.Format("{0}", i + 1);
            }
            this.dataGridView1.Refresh();
            GC.Collect();
        }

        /// <summary>
        /// 开始计算
        // </summary>
        /// <param name="sender"></param> 
        /// <param name="e"></param>
        private void comput_Click_1(object sender, EventArgs e)
        {
            //获得行数
            int rowCount = 0;
            while(this.dataGridView1.Rows[rowCount].Cells[0].Value != null)
            {
                rowCount++;
            }
            if (rowCount==0) //当输入为空时
            {
                MessageBox.Show("数据不能为空","错误提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                return;
            }
            ArrayList alCompany = new ArrayList(); //投标单位
            List<double> alPrice = new List<double>(); //价格 

            //获取值
            try
            {
                double price = 0;
                for (int i = 0; i < rowCount; i++)
                {
                    alCompany.Add(this.dataGridView1.Rows[i].Cells[0].Value.ToString().TrimEnd().TrimStart());
                    if (double.TryParse(this.dataGridView1.Rows[i].Cells[1].Value.ToString().TrimEnd().TrimStart(), out price))
                    {
                        alPrice.Add(price);
                    }
                    else //当输入的价格不是数字是
                    {
                        while (true)
                        {
                            MessageBox.Show("第" + i + 1 + "行投标价输入有误","错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return;
                        }
                    }               
                }    
            }
            catch (Exception)
            {
                MessageBox.Show("数据不能为空","错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
                    
            
            //判断投标单位数量的区间
            char M ;
            if(alCompany.Count<10)
            {
                M = 'a';
            }
            else if(alCompany.Count < 20 && alCompany.Count>=10)
            {
                M = 'b';
            }
            else if(alCompany.Count < 30 && alCompany.Count >= 20)
            {
                M = 'c';
            }
            else
            {
                M = 'd';
            }

            ComputeAndOutupt cAO = new ComputeAndOutupt();
            switch (M)
            {        
                case 'a':               
                    cAO.Compute(alCompany, alPrice, dataGridView1, mBox1, mBox2, nBox1, nBox2, qzBox,qzBox, 0, 0,ref dgvCount);
                    break;

                case 'b':
                    cAO.Compute(alCompany, alPrice, dataGridView1, mBox1, mBox2, nBox1, nBox2, qzBox, qzBox, 1, 1, ref dgvCount);
                    break;

                case 'c':
                    cAO.Compute(alCompany, alPrice, dataGridView1, mBox1, mBox2, nBox1, nBox2, qzBox, qzBox, 2, 1, ref dgvCount);
                    break;

                case 'd':
                    cAO.Compute(alCompany, alPrice, dataGridView1, mBox1, mBox2, nBox1, nBox2, qzBox, qzBox, 3, 2, ref dgvCount);
                    break;

                default:
                    break;
            }
        }

        /// <summary>
        /// 导出到Excel中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnToExcel_Click(object sender, EventArgs e)
        {
            int rowCount = 0;
            while (this.dataGridView1.Rows[rowCount].Cells[0].Value != null)
            {
                rowCount++;
            }
            if (rowCount == 0) //当输入为空时
            {
                MessageBox.Show("数据不能为空","错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            InputAndOutputData pd = new InputAndOutputData();
            pd.ToExcel(this.dataGridView1,this.touBiaoPiCi, dgvCount);
            GC.Collect();
        }

        /// <summary>
        /// 清除数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClear_Click(object sender, EventArgs e)
        {
            this.dataGridView1.Rows.Clear();
            this.dataGridView1.RowCount = 30;

            //添加序号
            for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
            {
                DataGridViewRow r = this.dataGridView1.Rows[i];
                r.HeaderCell.Value = string.Format("{0}", i + 1);
            }
            this.dataGridView1.Refresh();
            GC.Collect();
        }

        /// <summary>
        /// 写入数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void paste_Click(object sender, EventArgs e)
        {
            InputAndOutputData pd = new InputAndOutputData();
            pd.Paste(dataGridView1);
            GC.Collect();
        }

        /// <summary>
        /// 获取nBox1的值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void nBox1_MouseClick(object sender, MouseEventArgs e)
        {
            nBox1.Text = "";
        }

        /// <summary>
        /// 获取nBox2的值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void nBox2_MouseClick(object sender, MouseEventArgs e)
        {
            nBox1.Text = "";
        }

        /// <summary>
        /// 获取mBox1的值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mBox1_MouseClick(object sender, MouseEventArgs e)
        {
            mBox1.Text = "";
        }

        /// <summary>
        /// 获取mBox2的值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mBox2_MouseClick(object sender, MouseEventArgs e)
        {
            mBox1.Text = "";
        }

        private void PingBiaoForm1_Load(object sender, EventArgs e)
        {
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
        }

        private void PingBiaoForm1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawRectangle(Pens.DarkOliveGreen, 0, 0, this.Width - 1, this.Height - 1);
        }
    }
}
